package com.fenye.testflow.controller;


import com.fenye.testflow.model.dto.ImageDto;
import com.fenye.testflow.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.io.IOException;

@RestController
@RequestMapping("/api")
public class ImageController {

    @Autowired
    private ImageService imageService;

    @GetMapping("/generate-image")
    public byte[] generateImage(
            @RequestParam(name = "width", defaultValue = "100") int width,
            @RequestParam(name = "height", defaultValue = "100") int height,
            @RequestParam(name = "type", defaultValue = "png") String type,
            @RequestParam(name = "content", defaultValue = "Test Image") String content,
            @RequestParam(name = "size", defaultValue = "0") long size) {

        // 创建请求模型
        ImageDto imageRequest = new ImageDto();
        imageRequest.setWidth(width);
        imageRequest.setHeight(height);
        imageRequest.setType(type);
        imageRequest.setContent(content);
        imageRequest.setSize(size);

        try {
            // 调用服务层生成图片
            return imageService.generateImage(imageRequest);
        } catch (IOException e) {
            e.printStackTrace();
            // 返回错误响应
            return new byte[0];
        }
    }
}
